package com.finger.statisticsservice.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.finger.statisticsservice.client.UcenterClient;
import com.finger.statisticsservice.entity.StatisticsDaily;
import com.finger.statisticsservice.entity.vo.EchartsQuerySearch;
import com.finger.statisticsservice.mapper.StatisticsDailyMapper;
import com.finger.statisticsservice.service.StatisticsDailyService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.apache.commons.lang3.RandomUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 网站统计日数据 服务实现类
 * </p>
 *
 * @author finger
 * @since 2022-01-29
 */
@Service
public class StatisticsDailyServiceImpl extends ServiceImpl<StatisticsDailyMapper, StatisticsDaily> implements StatisticsDailyService {

    @Autowired
    private UcenterClient ucenterClient;

    @Override
    public void updateRegisterCount(String day) {

        QueryWrapper<StatisticsDaily> statisticsDailyQueryWrapper = new QueryWrapper<>();

        statisticsDailyQueryWrapper.eq("date_calculated",day);

        baseMapper.delete(statisticsDailyQueryWrapper);

        Integer loginCount = ucenterClient.getLoginCount(day);

        StatisticsDaily statisticsDaily = new StatisticsDaily();

        statisticsDaily.setRegisterNum(loginCount);
        statisticsDaily.setDateCalculated(day);
        statisticsDaily.setLoginNum(RandomUtils.nextInt(100,200));
        statisticsDaily.setVideoViewNum(RandomUtils.nextInt(100,200));
        statisticsDaily.setCourseNum(RandomUtils.nextInt(100,200));

        baseMapper.insert(statisticsDaily);
    }

    @Override
    public Map<String,Object> getEchartsDate(EchartsQuerySearch echartsQuerySearch) {

        QueryWrapper<StatisticsDaily> statisticsDailyQueryWrapper = new QueryWrapper<>();

        statisticsDailyQueryWrapper.select(echartsQuerySearch.getType(),"date_calculated");

        statisticsDailyQueryWrapper.between("date_calculated",echartsQuerySearch.getBegin(),echartsQuerySearch.getEnd());

        statisticsDailyQueryWrapper.orderByAsc("date_calculated");
        List<StatisticsDaily> dayList = baseMapper.selectList(statisticsDailyQueryWrapper);

        Map map = new HashMap<String,Object>();

        List dataList = new ArrayList<Integer>();

        List dateList = new ArrayList<String>();

        map.put("dataList",dataList);
        map.put("dateList",dateList);

        for (StatisticsDaily statisticsDaily : dayList) {

            // 添加日期
            dateList.add(statisticsDaily.getDateCalculated());
            switch (echartsQuerySearch.getType()){
                case "register_num":
                    dataList.add(statisticsDaily.getRegisterNum());
                    break;
                case "login_num":
                    dataList.add(statisticsDaily.getLoginNum());
                    break;
                case "video_view_num":
                    dataList.add(statisticsDaily.getVideoViewNum());
                    break;
                case  "course_num":
                    dataList.add(statisticsDaily.getCourseNum());
                    break;
                default:
                    break;
            }
        }


        return map;
    }
}
